import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      title: 'Widget',
      routes: <String, WidgetBuilder> {
        'counterWidget': (context)=>CounterWidget(initvalue: 30,)
      },
//      home: new Echo(title: '😆😁👌', backgroundColor: Colors.redAccent,),
    home: new Echo('😆😁👌11'),
    );
  }
}

class Echo extends StatelessWidget {

  //下面两种方式，主要选填和必填的使用

//  const Echo({
//    Key key,
//    @required this.title,
//    this.backgroundColor: Colors.grey,
//  }) : super(key: key);

  const Echo(this.title , [this.backgroundColor = Colors.grey]): super();

  final String title;
  final Color backgroundColor;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('${title != null ? title : 'router'}'),
      ),
      body: new Center(
        child: new Container(
          color: backgroundColor,
          child: FlatButton(
            child: new Text('push ${title != null ? title : 'router'}', style: TextStyle(fontSize: 30, color: Colors.blue),),
            onPressed: () {
              Navigator.pushNamed(context, 'counterWidget');
            },
          ),
        )
      ),
    );
  }
}



class CounterWidget extends StatefulWidget {
  const CounterWidget({Key key, this.initvalue: 0});

  final int initvalue;

  @override
  State<StatefulWidget> createState() {
    return _CounterWidgetState();
  }
}


class _CounterWidgetState extends State<CounterWidget> {

  int _counter;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _counter = widget.initvalue;
    print('初始化状态 initValue = $_counter');
  }
  
  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
    print('didChangeDependencies');
  }

  @override
  void didUpdateWidget(CounterWidget oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
    print('didUpdateWidget');
  }

  @override
  void reassemble() {
    // TODO: implement reassemble
    super.reassemble();
    print('reassemble');
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    print('build');
    return new Scaffold(
      appBar: new AppBar(

      ),
      body: Center(
        child: new Text('$_counter', style: TextStyle(fontSize: 44, color: Colors.blue),)
      ),
      
      floatingActionButton: new FloatingActionButton(
        child: new Icon(Icons.add),
        onPressed: () {
          setState(() {
            _counter++;
            print('setState $_counter');
          });
        },
      ),
    );
  }

  @override
  void deactivate() {
    // TODO: implement deactivate
    super.deactivate();
    print('deactivate');
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    print('dispose');
  }
}
